package com.hzj.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.recipe.entity.Comment;
import com.hzj.recipe.entity.Recipe;
import com.hzj.recipe.entity.User;
import com.hzj.recipe.mapper.CommentMapper;
import com.hzj.recipe.mapper.RecipeMapper;
import com.hzj.recipe.mapper.UserMapper;
import com.hzj.recipe.resp.ReplyResp;
import com.hzj.recipe.resp.commentResp;
import com.hzj.recipe.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 蜗牛菌
 * @description 针对表【comment(评论表)】的数据库操作Service实现
 * @createDate 2024-01-04 10:49:47
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RecipeMapper recipeMapper;

    @Override
    public Object listAll(Integer index, Integer size, Comment comment) {
        //默认第一页开始，每页5条
        if (index == null || index == 0) {
            index = 1;
        }
        if (size == null || size == 0) {
            size = 5;
        }
        Page<Comment> pageInfo = new Page(index, size);
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (comment.getCommentId() != null && comment.getCommentId() != 0 && !Objects.equals(comment.getCommentId(), "")) {
            lambdaQueryWrapper.eq(Comment::getCommentId, comment.getCommentId());
        }
        if (comment.getRecipeId() != null && comment.getRecipeId() != 0 && !Objects.equals(comment.getRecipeId(), "")) {
            lambdaQueryWrapper.eq(Comment::getRecipeId, comment.getRecipeId());
        }
        if (comment.getUserId() != null && comment.getUserId() != 0 && !Objects.equals(comment.getUserId(), "")) {
            lambdaQueryWrapper.eq(Comment::getUserId, comment.getUserId());
        }
        if (comment.getCommentParentId() != null && comment.getCommentParentId() != 0 && !Objects.equals(comment.getCommentParentId(), "")) {
            lambdaQueryWrapper.eq(Comment::getCommentParentId, comment.getCommentParentId());
        }
        if ((comment.getCommentContent() != null) && !Objects.equals(comment.getCommentContent(), "")) {
            lambdaQueryWrapper.like(Comment::getCommentContent, comment.getCommentContent());
        }
        if (comment.getCommentStatus() != null && comment.getCommentStatus() != 0 && !Objects.equals(comment.getCommentStatus(), "")) {
            lambdaQueryWrapper.eq(Comment::getCommentStatus, comment.getCommentStatus());
        }

        Page<Comment> commentPage = commentMapper.selectPage(pageInfo, lambdaQueryWrapper);
        return commentPage;
    }

    @Override
    public List<commentResp> listAllByRecipeId(Integer index, Integer size, Serializable recipeId) {
        Integer start = (index - 1) * size;

        //临时评论列表
        List<Comment> commentList = commentMapper.selectCommentsByRecipeId(start, size, recipeId);
        System.out.println(commentList);

        if (commentList != null) {
            //评论列表
            List<commentResp> commentRespList = new ArrayList<commentResp>();

            for (Comment comment : commentList) {
                //评论
                commentResp commentResp1 = new commentResp();
                commentResp1.setComment(comment);

                //评论用户user
                User user = userMapper.selectById(comment.getUserId());
                commentResp1.setUser(user);

                //临时回复列表
                List<Comment> replyList1 = commentMapper.selectCommentsByCommentParentId(recipeId, comment.getCommentId());
                if (replyList1.size() != 0) {
                    //不为空才创建回复列表
                    List<commentResp> replyList = new ArrayList<commentResp>();

                    //单个回复
                    for (Comment reply : replyList1) {
                        commentResp commentResp2 = new commentResp();
                        commentResp2.setComment(reply);

                        User user2 = userMapper.selectById(reply.getUserId());
                        commentResp2.setUser(user2);

                        //回复对象不为空
                        if (reply.getTargetId() != null) {
                            User target = userMapper.selectById(reply.getTargetId());
                            commentResp2.setTarget(target);
                        }
                        //添加进回复列表
                        replyList.add(commentResp2);
                    }
                    //将回复列表————》返回类
                    commentResp1.setReplyList(replyList);
                }
                //将返回类————》返回类集合
                commentRespList.add(commentResp1);
            }
            return commentRespList;
        }
        return null;
    }

    @Override
    public List<ReplyResp> listAllByCommentParentId(Integer index, Integer size, Serializable targetId) {
        Integer start = (index - 1) * size;

        //临时回复列表
        List<Comment> commentList = commentMapper.selectReplysByCommentParentId(start, size, targetId);
        System.out.println(commentList);
        if (commentList != null) {
            //回复列表
            List<ReplyResp> replyRespList = new ArrayList<ReplyResp>();

            for (Comment comment : commentList) {
                //回复单体
                ReplyResp replyResp = new ReplyResp();

                //“我”的评论
                Comment comment1 = commentMapper.selectById(comment.getCommentParentId());
                replyResp.setComment(comment1);

                //他人的用户信息
                User user1 = userMapper.selectById(comment.getUserId());
                replyResp.setUser(user1);

                //他人对“我”评论的回复
                replyResp.setReply(comment);

                //食谱信息
                Recipe recipe = recipeMapper.selectById(comment.getRecipeId());
                replyResp.setRecipe(recipe);
                replyRespList.add(replyResp);
            }
            return replyRespList;
        }
        return null;
    }
}




